package com.ty.dictionaries.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ty.dictionaries.listener.DictListener;
import com.ty.dictionaries.mapper.DictMapper;
import com.ty.dictionaries.service.IDictService;
import com.ty.utils.pojo.Dict;
import com.ty.utils.pojo.vo.DictVo;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 数据管理表 服务实现类
 * </p>
 *
 * @author xinyu
 * @since 2023-02-20
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements IDictService {
    
    @Resource
    private DictMapper dictMapper;
    
    //根据数据id查询子数据列表
    @Cacheable(value = "dict",keyGenerator = "keyGenerator")
    @Override
    public List<Dict> findChlidData(Long id) {

        //创建QueryWrapper
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        //条件
        queryWrapper.eq("parent_id", id);
        //根据parent_id 查询父类
        List<Dict> dictList = dictMapper.selectList(queryWrapper);
        //向list集合每个dict 对象中设置hasChildren
        for (Dict dict : dictList) {
            Long dictId = dict.getId();
            boolean isChild = this.isChildren(dictId);
             dict.setHasChildren(isChild);
        }

        return dictList;
    }
    /*导出*/
    @Override
    public void exportData(HttpServletResponse response) {
        try{
            response.setContentType("application/vnd.ms-excel");
            //中文防止乱码
            response.setCharacterEncoding("utf-8");
            //设置名称
            String fileName = URLEncoder.encode("数据字典");
            response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");

            List<Dict> dictList = dictMapper.selectList(null);
            List<DictVo> dictVoList=new ArrayList<>(dictList.size());
            for(Dict dict:dictList){
                DictVo dictVo=new DictVo();
                //copy dict数据到dictVo
                BeanUtils.copyProperties(dict, dictVo, DictVo.class);
                dictVoList.add(dictVo);
            }
            //导出 写
            EasyExcel.write(response.getOutputStream(),DictVo.class).sheet("数据字典").doWrite(dictList);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    @CacheEvict(value = "dict",allEntries = true)
    /*数据字典导入*/
    @Override
    public void importData(MultipartFile file) {
        try{
            EasyExcel.read(file.getInputStream(),DictVo.class,new DictListener(baseMapper)).sheet().doRead();
        }catch (IOException e){
            e.printStackTrace();
        }
    }


    //判断id下面是否有字节点
    private boolean isChildren(Long id){
        QueryWrapper<Dict> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("parent_id",id);
        Long count = dictMapper.selectCount(queryWrapper);
        return count>0;
    }
}
